<?php
namespace Common\Lib;
class Category {
	
	//传递一个父级分类ID返回所有子级分类
	public static function subtree($arr,$id){
		//将最后的结果存放在一个数组里
		$subs=array();                                                  
		//循环数组
		foreach($arr as $v){ 
			//判断参数id是否等于父id
			if($v['parentid']==$id){                                      
				//追加到tree数组
				$subs[]=$v;  
				//合并数组
				$subs=array_merge($subs,self::subtree($arr,$v['cateid']));      
			}
		}
		return $subs;  //返回数组
	}

	public static function make_tree($list,$pk='cateid',$pid='parentid',$child='_child',$root=0){
		$tree=array();
		foreach($list as $key => $val){
			if($val[$pid] == $root){
				//获取当前$pid所有子类 
				unset($list[$key]);
				if(! empty($list)){
					$child = self::make_tree($list,$pk,$pid,$child,$val[$pk]);
					if(!empty($child)){
						$val['children'] = $child;
					}
				}
				$value['catename'] = $val['catename'];
				$value['cateid'] = $val['cateid'];
				$value['cate_url'] = $m.$val['cate_url'];
				$value['children'] = $val['children'];
				$tree[] = $value;
			}
		}
		return $tree;
	}
	//栏目url
	public static function make_url($list){
		//获取操作模块名
		$m=MODULE_NAME;
		foreach($list as $k=>$v){
			if(strstr("{$list[$k]['cate_url']}","index?")){
				if(strstr("{$list[$k]['cate_url']}","Home/")){
					$list[$k]['cate_url']=$list[$k]['cate_url'];
				}else{
					//动态地址
					if($m == 'Home' || $m == 'Admin'){
						//静态地址
						$list[$k]['cate_url']="/Home".$list[$k]['cate_url'];
					}else{
						//静态地址
						$list[$k]['cate_url']="/mobile".$list[$k]['cate_url'];
					}
				}
			}else{
				if($m == 'Home' || $m == 'Admin'){
					if(strstr("{$list[$k]['cate_url']}","www/")){
						//静态地址
						$list[$k]['cate_url']=$list[$k]['cate_url'];
					}else{
						//静态地址
						$list[$k]['cate_url']="/www".$list[$k]['cate_url'];	
					}
					
				}else{
					if(strstr("{$list[$k]['cate_url']}","m/")){
						//静态地址
						$list[$k]['cate_url']=$list[$k]['cate_url'];
					}else{
						//静态地址
						$list[$k]['cate_url']="/m".$list[$k]['cate_url'];
					}
					
				}
				
			}
		}
		return $list;
	}
	//文章url
	public static function url($list){
		//获取操作模块名
		$m=MODULE_NAME;
		foreach($list as $k=>$v){
			if(strstr("{$list[$k]['url']}","index?")){
				//动态地址
				if($m == 'Home' || $m == 'Admin'){
					//静态地址
					$list[$k]['url']="/Home".$list[$k]['url'];
				}else{
					//静态地址
					$list[$k]['url']="/mobile".$list[$k]['url'];
				}
			}else{
				if($m == 'Home' || $m == 'Admin'){
					//静态地址
					$list[$k]['url']="/www".$list[$k]['url'];
				}else{
					//静态地址
					$list[$k]['url']="/m".$list[$k]['url'];
				}
			}
		}
		return $list;
	}

	// 传递一个子分类ID返回他的所有父级分类
	public static function getParents($cate, $id) {
		$arr = array();
		$cate=self::make_url($cate);
		foreach ($cate as $v) {
			if ($v['cateid'] == $id) {
				//面包屑方法，只需要catename字段
				$arr[] = '<a href="'.$v['cate_url'].'">'.$v['catename'].'</a>';
				$arr = array_merge(self::getParents($cate, $v['parentid']), $arr);
			}
		}
		return $arr;
	}
}
?>